package com.dy.filter;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther zzyy
 * @create 2020-02-21 16:40
 */
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered
{
    @Autowired(required = false) // 这个可以解决idea报错
    @Qualifier("restTemplate") // 表示根据名称来找bean
    private RestTemplate restTemplate;
    private  String tokenCheck="lb://auth/oauth/check_token?token={token}";
    /**
     * auth模块的routerId
     */
    private static final String AUTH_CLIENT = "DiscoveryClient_AUTH";

    @SneakyThrows
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
    {

        log.info("***********come in MyLogGateWayFilter:  "+new Date());

        Route gatewayRoute = exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
        ServerHttpRequest request = exchange.getRequest();
        log.info("===========================request begin================================================");
        log.info("URI         : {}", request.getURI());
        log.info("Method      : {}", request.getMethod());
        log.info("Headers     : {}", request.getHeaders());
        log.info("Headers     : {}", request.getBody());
        log.info("==========================request end================================================");
        List<PathContainer.Element> elements = request.getPath().elements();
        System.out.println(gatewayRoute);
        if (null != gatewayRoute && AUTH_CLIENT.equalsIgnoreCase(gatewayRoute.getId())) {
            // 表示auth模块 直接放行
            return chain.filter(exchange);
        }
        boolean isAuth = elements.stream().anyMatch(e -> "oauth".equals(e.value()));
        // 表示不含 api 的请求 直接放行
        if (isAuth) {
            return chain.filter(exchange);
        }

//        String uname = exchange.getRequest().getQueryParams().getFirst("id");
        String token=exchange.getRequest().getHeaders().getFirst("Authorization");
        String url= null;
        url=exchange.getRequest().getURI().toURL().toString();
        log.info("iiiii"+url);
        System.out.println("获取ddd"+url);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token",token);
        System.out.println(token);
        HttpEntity httpEntity = new HttpEntity<>(null, httpHeaders);
        Map ops=new HashMap<>();
        try {
           ops= restTemplate.exchange(tokenCheck, HttpMethod.GET, httpEntity, Map.class,token).getBody();
        } catch (RestClientException e) {
            e.getRootCause();
            System.out.println("***********1爱上一片名你**************");
            System.out.println(e.getCause());
            System.out.println("***********2爱上一片名你**************");
            System.out.println(e.getRootCause());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        String errorKey = "error";
        System.out.println(ops);
        if (ops.containsKey(errorKey)) {
            if (log.isDebugEnabled()) {
                log.debug("user info returned error: " + ops.get("error"));
            }
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, ops.get("error").toString());
        }
//        if(uname == null)
//        {
//            log.info("*******用户名为null，非法用户，o(╥﹏╥)o");
//            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
//            return exchange.getResponse().setComplete();
//        }

        return chain.filter(exchange);
    }


    @Override
    public int getOrder()
    {
        return 0;
    }
}
